package everyday;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;

public class LocalIOUtil {

    /**
     * 读取本地文件到string
     */
    public static String readLocal2String(String localPath) {
        StringBuilder sb = new StringBuilder();
        try (BufferedReader br = new BufferedReader(new FileReader(localPath))) {
            String lineStr;
            while ((lineStr = br.readLine()) != null) {
                sb.append(lineStr);
            }
            return sb.toString();
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("读取本地文件异常");
        }
    }

    /**
     * string输出到本地文件
     *
     * @param str       字符串内容
     * @param localPath 本地路径
     */
    public static void output2local(String str, String localPath) {
        System.out.println("string输出到本地文件-开始");
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(localPath))) {
            writer.write(str);
            System.out.println("string输出到本地文件-成功");
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("string输出到本地文件异常");
        }
    }
}